/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Commons.Replicazione;


import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.ws.WebServiceRef;
import replication.RManagerWS;
import replication.RManagerWSService;



/**
 *
 * @author drag0nman
 * 
 */


public class WSClientRMDiscovery  implements Runnable{
@WebServiceRef(wsdlLocation="http://192.168.1.4:8080/ReplicationApp/RManagerWSService?wsdl")
RManagerWSService service;

 globalmonitor.Notifica notificaDaInviare;

public WSClientRMDiscovery()
{
    service = new RManagerWSService();

 }

 

    @Override
    public void run() {
     
    }

   

    public boolean inviaStato(StatoDiscovery m) {
          RManagerWS p = service.getRManagerWSPort();
          byte [] arrayDaInviare = creaArray(m);
          return  p.inviaStato(arrayDaInviare,m.getId());
    }

     public StatoDiscovery bootstrap(String id)
    {
        RManagerWS p = service.getRManagerWSPort();
        byte [] byteStato = p.bootstrap(id);
        StatoDiscovery statoD = ricostruisciStato(byteStato, id);
        return statoD;
    }

    private StatoDiscovery ricostruisciStato(byte[] bytes,String id) {
       StatoDiscovery stato = null;
       String fileName = null;
        try
        {
            fileName = id + "temp.xml";
            FileOutputStream out = new FileOutputStream(fileName);
            out.write(bytes);
            out.flush();
            out.close();
        }
        catch(Exception e)
        {
           e.printStackTrace();
        }
        return creaStatoDiscovery(new File(fileName));
    }



    private StatoDiscovery creaStatoDiscovery(File file) {
        StatoDiscovery statoD=null;
        try {
            XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(file)));
            statoD= (StatoDiscovery)decoder.readObject();
            decoder.close();
            file.delete();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(WSClientRMManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return statoD;
    }


    private byte[] creaArray(StatoDiscovery m) {

        XMLEncoder e;
        byte [] bytes=null;
        try {

            e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(m.getId() + ".xml")));
            e.writeObject(m);
            e.close();
            File file = new File(m.getId() +".xml");
            InputStream is = new FileInputStream(m.getId() +".xml");
            long length = file.length();
            bytes = new byte[(int)length];
            int offset = 0;
            int numRead = 0;
          while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }




        is.close();






        } catch (FileNotFoundException ex) {
            
        }

         catch (Exception ex) {

        }
          return bytes;

    }

}


